import { Injectable } from '@nestjs/common';
import { QueryProductVo } from "../../vo/product/query-product.vo";
import { ResultVo } from "../../vo/common/result.vo";
import { InjectRepository } from "@nestjs/typeorm";
import { ProductEntity } from "./product.entity";
import { Repository } from "typeorm";
import { ProductStatusEnum } from "../../enum/product-status.enum";
import { PageResVo } from "../../vo/common/page.res.vo";
import { ProductListResVo } from "../../vo/product/product-list-res.vo";
import { QueryDetailReqVo } from 'src/vo/product/query-detail-req.vo';
import { ApiCodeEnum } from 'src/enum/api-code.enum';

@Injectable()
export class ProductService {

    constructor(
        @InjectRepository(ProductEntity)
        private readonly productRepository: Repository<ProductEntity>,
    ) { }

    async queryDetail(queryDetailReqVo: QueryDetailReqVo) {
        console.log("查询商品详情:", queryDetailReqVo);
        let product = await this.productRepository.findOne({ id: queryDetailReqVo.productId })
        if (!product) return ResultVo.exception(ApiCodeEnum.PRODUCT_NOT_EXIST)
        return ResultVo.successWithData(product)
    }

    async queryProductsByPage(queryProductVo: QueryProductVo) {
        console.log("queryProductsByPage：", queryProductVo)
        let qb = this.productRepository.createQueryBuilder()
        // 拼接条件
        let query = new ProductEntity()
        query.status = ProductStatusEnum.SALE_ON
        if (queryProductVo.cateId) {
            query.cateId = queryProductVo.cateId
        }
        if (queryProductVo.isHot == 1) {
            query.isHot = queryProductVo.isHot
        }
        qb.where(query as Partial<ProductEntity>)
        // 自定义查询
        if (queryProductVo.productName) {
            qb.andWhere("product_name like :productName", { productName: '%' + queryProductVo.productName + '%' })
        }
        // 排序
        if (queryProductVo.orderField && queryProductVo.direction) {
            qb.addOrderBy(queryProductVo.orderField, queryProductVo.direction as "ASC" | "DESC")
        }
        // 分页
        qb.skip(queryProductVo.size * (queryProductVo.page - 1)).take(queryProductVo.size)
        // 查询数据
        let [resData, total] = await qb.getManyAndCount()
        // 实体转化
        let voList = this.productEntityListToVoList(resData);
        // 构建分页返回公共参数
        let pageResVo = PageResVo.generatePage(queryProductVo, total, voList);
        return ResultVo.successWithData(pageResVo)
    }

    private productEntityListToVoList(resData: ProductEntity[]) {
        // let list = []
        let list: ProductListResVo[] = JSON.parse(JSON.stringify(resData))
        // resData.forEach(entity => {
        //     let vo = new ProductListResVo()
        //     vo.id = entity.id
        //     vo.productName = entity.productName
        //     vo.cateId = entity.cateId
        //     vo.desc = entity.desc
        //     vo.headImg = entity.headImg
        //     vo.salePrice = entity.salePrice
        //     vo.originalPrice = entity.originalPrice
        //     vo.cateName = "暂无"
        //     list.push(vo)
        // })
        return list
    }
}
